package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.OrderUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.SysMealOrder;
import com.ruoyi.system.domain.SysMealOrderForm;
import com.ruoyi.system.domain.SysMealStatistics;
import com.ruoyi.system.domain.vo.SysMealOrderVO;
import com.ruoyi.system.mapper.SysMealOrderMapper;
import com.ruoyi.system.mapper.SysMealStatisticsMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysMealOrderService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 报餐订单Service业务层处理
 *
 * @author jijinwu
 * @date 2024-07-19
 */
@Service
public class SysMealOrderServiceImpl implements ISysMealOrderService
{
    private static final Logger log = LoggerFactory.getLogger(SysMealOrderServiceImpl.class);

    @Autowired
    private SysMealOrderMapper sysMealOrderMapper;

    @Autowired
    private SysUserMapper userMapper;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Autowired
    private SysMealStatisticsMapper sysMealStatisticsMapper;

    /**
     * 查询报餐订单
     *
     * @param orderId 报餐订单主键
     * @return 报餐订单
     */
    @Override
    public SysMealOrder selectSysMealOrderByOrderId(Long orderId)
    {
        return sysMealOrderMapper.selectSysMealOrderByOrderId(orderId);
    }

    /**
     * 查询报餐订单列表
     *
     * @param sysMealOrder 报餐订单
     * @return 报餐订单
     */
    @Override
    public List<SysMealOrder> selectSysMealOrderList(SysMealOrder sysMealOrder)
    {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        SysMealOrderForm sysMealOrderForm = new SysMealOrderForm();
        sysMealOrderForm.setOrderDate(sysMealOrder.getOrderDate());
        sysMealOrderForm.setOrderStatus(sysMealOrder.getOrderStatus());
        sysMealOrderForm.setPageNum(pageNum);
        sysMealOrderForm.setPageSize(pageSize);
        return sysMealOrderMapper.selectSysMealOrderList(sysMealOrderForm);
    }

    @Override
    public List<SysMealOrderVO> selectSysMealOrderVOList(SysMealOrder sysMealOrder)
    {
        return sysMealOrderMapper.selectSysMealOrderVOList(sysMealOrder);
    }


    /**
     * 新增报餐订单
     *
     * @param sysMealOrder 报餐订单
     * @return 结果
     */
    @Override
    public int insertSysMealOrder(SysMealOrder sysMealOrder)
    {
        return sysMealOrderMapper.insertSysMealOrder(sysMealOrder);
    }

    /**
     * 修改报餐订单
     *
     * @param sysMealOrder 报餐订单
     * @return 结果
     */
    @Override
    public int updateSysMealOrder(SysMealOrder sysMealOrder)
    {
        return sysMealOrderMapper.updateSysMealOrder(sysMealOrder);
    }

    /**
     * 批量删除报餐订单
     *
     * @param orderIds 需要删除的报餐订单主键
     * @return 结果
     */
    @Override
    public int deleteSysMealOrderByOrderIds(Long[] orderIds)
    {
        return sysMealOrderMapper.deleteSysMealOrderByOrderIds(orderIds);
    }

    /**
     * 删除报餐订单信息
     *
     * @param orderId 报餐订单主键
     * @return 结果
     */
    @Override
    public int deleteSysMealOrderByOrderId(Long orderId)
    {
        return sysMealOrderMapper.deleteSysMealOrderByOrderId(orderId);
    }

    @Override
    public int saveMealOrderLog(LoginUser loginUser,Long[] userIds,Long canteenId,String mealDate) {
        List<SysUser> sysUserList = userMapper.selectByIds(userIds);
        SysDept company = deptService.getCompany(loginUser.getDeptId());
        String uuid = IdUtils.simpleUUID();
        List<SysMealOrder> sysMealOrderList = new ArrayList<>();
        if (sysUserList.size() > 0){
            for (SysUser sysUser : sysUserList) {
                SysMealOrder sysMealOrder = new SysMealOrder();
                sysMealOrder.setUserId(sysUser.getUserId());
                sysMealOrder.setOrderNumber(OrderUtils.getOrderCode(sysUser.getUserId()));
                sysMealOrder.setDeptId(sysUser.getDeptId());
                sysMealOrder.setCompanyId(company.getDeptId());
                sysMealOrder.setOrderStatus("0");
                sysMealOrder.setOrderDate(DateUtils.getNowDate());
                sysMealOrder.setCreateBy(loginUser.getUsername());
                sysMealOrder.setCreateTime(DateUtils.getNowDate());
                sysMealOrder.setMealUuid(uuid);
                sysMealOrder.setCanteenId(canteenId);
                sysMealOrder.setMealDate(DateUtils.parseDate(mealDate));
                sysMealOrderList.add(sysMealOrder);
            }
        }

        int count = 0;
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH,false);
        SysMealOrderMapper sysMealOrderMapper1 = sqlSession.getMapper(SysMealOrderMapper.class);
        for (SysMealOrder sysMealOrder : sysMealOrderList) {
            sysMealOrderMapper1.insertSysMealOrder(sysMealOrder);
            count ++;
        }
        log.info("提交报餐人数结果:->{}",count);
        sqlSession.commit();
        sqlSession.clearCache();
        //保存报餐统计记录
        saveSysMealStatistics(loginUser.getUserId(),loginUser.getDeptId(),
                company.getDeptId(),count,uuid);
        log.info("报餐成功");
        return count;
    }

    @Override
    public List<SysMealOrderVO> listOrderByUserId(Long userId) {
        return sysMealOrderMapper.listOrderByUserId(userId);
    }

    @Override
    public Integer cancelOrder(String orderNumber) {
        return sysMealOrderMapper.cancelOrder(orderNumber);
    }


    public void saveSysMealStatistics(Long userId,Long deptId,Long companyId,int count,String uuid){
        SysMealStatistics sysMealStatistics = new SysMealStatistics();
        sysMealStatistics.setUserId(userId);
        sysMealStatistics.setDeptId(deptId);
        sysMealStatistics.setCompanyId(companyId);
        sysMealStatistics.setCount(count);
        sysMealStatistics.setOrderTime(DateUtils.getNowDate());
        sysMealStatistics.setMealUuid(uuid);
        sysMealStatisticsMapper.insertSysMealStatistics(sysMealStatistics);
    }
}
